﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverTile.Xaml
{
    public class SpriteTile : FrameworkElement
    {

        public SpriteTile()
        { }

        public static readonly DependencyProperty XProperty = DependencyProperty.RegisterAttached("X", typeof(Int32), typeof(SpriteTile), null);
        public Int32 X
        {
            get { return (Int32)GetValue(XProperty); }
            set { SetValue(XProperty, value); }
        }

        public static readonly DependencyProperty YProperty = DependencyProperty.RegisterAttached("Y", typeof(Int32), typeof(SpriteTile), null);
        public Int32 Y
        {
            get { return (Int32)GetValue(YProperty); }
            set { SetValue(YProperty, value); }
        }

        //public static readonly DependencyProperty BlockingProperty = DependencyProperty.RegisterAttached("Blocking", typeof(Boolean), typeof(SpriteTile), null);
        private Boolean? _blocking;
        public Boolean Blocking
        {
            get
            {
                if (_blocking.HasValue)
                {
                    return _blocking.Value;
                }
                else if (Frame != null)
                {
                    return Frame.Blocking;
                }
                return false;
            }
            set { _blocking = value; }
        }

        public static readonly DependencyProperty FrameProperty = DependencyProperty.RegisterAttached("Frame", typeof(SpriteFrame), typeof(SpriteTile), null);
        public SpriteFrame Frame
        {
            get { return (SpriteFrame)GetValue(FrameProperty); }
            set { SetValue(FrameProperty, value); }
        }

        public static readonly DependencyProperty TransitionProperty = DependencyProperty.RegisterAttached("Transition", typeof(AreaTransition), typeof(SpriteTile), null);
        public AreaTransition Transition
        {
            get { return (AreaTransition)GetValue(TransitionProperty); }
            set { SetValue(TransitionProperty, value); }
        }

        public static readonly DependencyProperty SpawnPointProperty =
            DependencyProperty.Register("SpawnPoint", typeof(SpawnPoint), typeof(SpriteTile), 
            new PropertyMetadata(SpawnPointChanged));
        public SpawnPoint SpawnPoint
        {
            get { return (SpawnPoint)GetValue(SpawnPointProperty); }
            set { SetValue(SpawnPointProperty, value); }
        }

        private static void SpawnPointChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
        {
            SpawnPoint spawnPoint = (SpawnPoint)e.NewValue;
            spawnPoint.Tile = (SpriteTile)d;
        }

    }
}
